package com.summer.study.leetcode.simple_class;

/**
 * @auther: 杨杰
 * @date: 2019/7/26 14:52
 * @description: 单链表
 */
public class SinglyLinkedList<E> {

    public Node head, tail;

    public int size;

    public SinglyLinkedList() {
        head = null;
        tail = head;
        size = 0;
    }

    public boolean add(E e) {
        Node node = new Node(e);
        if (tail == null) {
            tail = new Node(null);
            head = node;
        }
        tail.next = node;
        tail = node;
        size++;
        return true;
    }

    public void transfer() {
        Node node = head;
        while (node.next != null) {
            System.out.println(node.next.val);
            node = node.next;
        }
    }


    public void transfer2() {
        Node node = tail;
        while (node != null) {
            node = node.next;
            if (node != null) {
                System.out.println(node.val);
            } else {
                System.out.println("NULL");
            }
        }
    }




    public Node<E> reverseList(Node<E> head) {
        Node curr = head;
        Node prev = null;
        while (curr != null) {
            Node nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return curr;
    }

    class Node<E> {

        E val;

        Node next;

        public Node(E x) {
            val = x;
        }
    }

}
